/**
 * Created by zhaojm on 15/3/21.
 */
game.GameOverLayer = cc.Layer.extend({

    ctor:function(data){
        this._super();
        that = this;
        cc.spriteFrameCache.addSpriteFrames(res.gameover_plist);
        this._spriteSheet = new cc.SpriteBatchNode(res.gameover_png);
        this.addChild(this._spriteSheet);

        //var lastData = sys.localStorage.getItems()

        //var tempDollNum = JSON.stringify(dollNum);

        var shareScore = data.scoreNum.toFixed(0);
        var maxScore = 1500;
        var sharePercent  = (shareScore / maxScore).toFixed(4);
        if(shareScore >= maxScore){
            sharePercent = 0.9998;
        }

        if(sharePercent < 0.5){
            stit = '恭喜你，跑了' + shareScore + '米，超过了 ' + sharePercent * 100 + '% 的玩家！继续努力吧！';
        }else if(sharePercent < 0.7){
            stit = '恭喜你，跑了' + shareScore + '米，超过了 ' + sharePercent * 100 + '% 的玩家！成绩不错哦！';
        }else if(sharePercent < 0.8){
            stit = '恭喜你，跑了' + shareScore + '米，超过了 ' + sharePercent * 100 + '% 的玩家！简直无敌了！';
        }else {
            stit = '恭喜你，跑了' + shareScore + '米，超过了 ' + sharePercent * 100 + '% 的玩家！神一般的存在！';
        }

        cc.log(stit);










        // TODO 构建界面


        var background = new cc.Sprite(res.gameover_bg_jpg);
        this.addChild(background);
        //background.setAnchorPoint(cc.p(0, 0));
        background.setPosition(cc.p(cc.winSize.width / 2, cc.winSize.height / 2));


        var text = new cc.Sprite('#level1.png');
        text.setPosition(cc.p(cc.winSize.width * 0.5, cc.winSize.height * 0.79));
        this.addChild(text);
        //
        //var text1 = new cc.Sprite(cc.spriteFrameCache.getSpriteFrame('text.png'));
        //text1.setPosition(cc.p(cc.winSize.width * 0.6, cc.winSize.height * 0.57));
        //this.addChild(text1);

        //var sun = new cc.Sprite(cc.spriteFrameCache.getSpriteFrame('sun.png'));
        //sun.setPosition(cc.p(cc.winSize.width * 0.3, cc.winSize.height * 0.74));
        //sun.setScale(0.4);
        //sun.setAnchorPoint(cc.p(1, 0.5));
        //this.addChild(sun);
        //
        //var coin = new cc.Sprite(cc.spriteFrameCache.getSpriteFrame('coin.png'));
        //coin.setPosition(cc.p(cc.winSize.width * 0.3, cc.winSize.height * 0.57));
        //coin.setAnchorPoint(cc.p(1, 0.5));
        //this.addChild(coin);




        //var gameover = new cc.Sprite(cc.spriteFrameCache.getSpriteFrame('gameover.png'));
        //this.addChild(gameover);
        ////gameover.setAnchorPoint(cc.p(0, 0));
        //gameover.setPosition(cc.p(cc.winSize.width / 2, cc.winSize.height / 2));
        //gameover.setScaleX(cc.winSize.width / gameover.getContentSize().width);

        //var score = data.scoreNum * game.Config.timeScore;
        //score += "";
        //cc.log(data.scoreNum * game.Config.timeScore);
        // 当前记录
        this.numScore = cc.LabelTTF.create('', 'Arial', 24);
        this.numScore.setColor(new cc.Color(130, 94, 42));
        this.numScore.setPosition(cc.p(cc.winSize.width * 0.52, cc.winSize.height * 0.58));
        this.addChild(this.numScore);
        this.numScore.setAnchorPoint(cc.p(0, 0.5));

// 最高纪录
        this.distance = cc.LabelTTF.create('', 'Arial', 24);
        this.distance.setColor(new cc.Color(130, 94, 42));
        this.distance.setPosition(cc.p(cc.winSize.width * 0.52, cc.winSize.height * 0.42));
        this.addChild(this.distance);
        this.distance.setAnchorPoint(cc.p(0, 0.5));


        var name = 'bear_data';
        var temp = cc.sys.localStorage.getItem(name);
        //将字符串转化为json
        temp = JSON.parse(temp);

        cc.log('temp==', temp, 'data=', data);


        if(temp == null || temp == "" || temp == 'undefined'){
            cc.sys.localStorage.setItem(name, JSON.stringify(data));
            text.setSpriteFrame(cc.spriteFrameCache.getSpriteFrame('level0.png'));
            // TODO 第一次
            cc.log('first time...');
            this.numScore.setString(data.scoreNum.toFixed(0) + 'm');
            this.distance.setString(data.scoreNum.toFixed(0) + 'm');

        }else{
            cc.log('not first time');


            if(temp.scoreNum < data.scoreNum) {
                cc.sys.localStorage.setItem(name, JSON.stringify(data));
                // TODO 覆盖 最高纪录
                cc.log('big score num ');
                this.numScore.setString(data.scoreNum.toFixed(0) + 'm');
                this.distance.setString(data.scoreNum.toFixed(0) + 'm');
                // 最高
                text.setSpriteFrame(cc.spriteFrameCache.getSpriteFrame('level0.png'));

            }else{
                cc.log('再接再厉。。');
                text.setSpriteFrame(cc.spriteFrameCache.getSpriteFrame('level1.png'));
                this.numScore.setString(data.scoreNum.toFixed(0) + 'm');

                if(temp.scoreNum){
                    this.distance.setString(temp.scoreNum.toFixed(0) + 'm');
                }else{
                    this.distance.setString(data.scoreNum.toFixed(0) + 'm');
                }

            }

        }


        var shareframe = cc.spriteFrameCache.getSpriteFrame("btn_1.png");
        var shareitem = new cc.MenuItemImage(shareframe,shareframe, shareframe, this.onShare, this);
        shareitem.setAnchorPoint(cc.p(1, 0.5));
        shareitem.setPosition(cc.p(cc.winSize.width * 0.5 - 2, cc.winSize.height * 0.23));



        var zz_continue = cc.spriteFrameCache.getSpriteFrame("btn_0.png");
        var item1 = new cc.MenuItemImage(zz_continue,zz_continue, zz_continue, this.onMenuCallback, this);
        item1.setAnchorPoint(cc.p(0 ,0.5));
        item1.setPosition(cc.p(cc.winSize.width * 0.5 + 2, cc.winSize.height * 0.23));



        var moreframe = cc.spriteFrameCache.getSpriteFrame("home.png");
        var moreitem = new cc.MenuItemImage(moreframe,moreframe, moreframe, this.onMore, this);
        moreitem.setPosition(cc.p(cc.winSize.width - 20, cc.winSize.height - 20));
        moreitem.setAnchorPoint(cc.p(1,1));
        //moreitem.setScale(0.5);


        var menu = new cc.Menu(shareitem, item1, moreitem);
        //menu.alignItemsVertically();
        this.addChild(menu);
        menu.setPosition( cc.p(0, 0));

    },

    onMenuCallback:function(){
        cc.director.runScene(new game.GameScene());
    },

    onMore:function(){
        window.location.href = "http://mingz.me";
    },

    onShare:function(){
        cc.log('on share...');
        if(game._Config.language == game._Enum.language.cn) {
            this.addChild(new game.ShareLayer());
        }
    },

});
